/* 
 * AP(r) Computer Science GridWorld Case Study:
 * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * @author Chris Nevison
 * @author Barbara Cloud Wells
 * @author Cay Horstmann
 */
import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

import java.awt.Color;
import java.util.ArrayList;

public class BlusterCritter extends Critter {
	private int courage_value;

	public BlusterCritter() {
		super();
		courage_value = 2;
	}

	public BlusterCritter(int c) {
		super();
		courage_value = c;
	}

	public ArrayList<Actor> getActors() {
		// TODO Auto-generated method stub
		ArrayList<Actor> actors = new ArrayList<Actor>();
		Location loc = getLocation();

		Grid gr = getGrid();
		for (int i = loc.getRow() - 2; i <= loc.getRow() + 2; i++) {
			for (int j = loc.getCol() - 2; j <= loc.getCol() + 2; j++) {
				Location temp = new Location(i, j);
				if (gr.isValid(temp)) {
					Actor actor = getGrid().get(temp);
					if (actor != null && actor != this) {
						actors.add(actor);
					}
				}
			}
		}

		return actors;
	}

	public void processActors(ArrayList<Actor> actors) {
		// TODO Auto-generated method stub
		// 统计周围动物数量
		int count = 0;
		for (Actor actor : actors) {
			if (actor instanceof Critter) {
				count++;
			}
		}
		if (count > courage_value) {
			Darken();
		} else {
			Brighten();
		}
	}

	private void Brighten() {
		Color color = getColor();
		int red = color.getRed();
		int green = color.getGreen();
		int blue = color.getBlue();

		// 变亮
		int high = 245;
		int inc = 10;
		if (red <= high) {
			red += inc;
		}
		if (green <= high) {
			green += inc;
		}

		if (blue <= high) {
			blue += inc;
		}

		setColor(new Color(red, green, blue));
	}

	private void Darken() {
		Color color = getColor();
		int red = color.getRed();
		int green = color.getGreen();
		int blue = color.getBlue();

		// 变暗
		int low = 10;
		int inc = 10;
		if (red >= low) {
			red -= inc;
		}
		if (green >= low) {
			green -= inc;
		}

		if (blue >= low) {
			blue -= inc;
		}

		setColor(new Color(red, green, blue));
	}
}
